<template>
  <view class="">
    <u-form-item :label="item.label" borderBottom :prop="item.value">
      <u-upload
        :disabled="disabled"
        ref="formitem"
        :fileList="fileList"
        @afterRead="afterRead"
        :maxCount="maxCount"
        :accept="accept"
        @delete="deletePic"
      ></u-upload>
      <!-- @afterRead="afterRead"
      @delete="deletePic" -->
    </u-form-item>
  </view>
</template>

<script>
import upload from "@/utils/upload.js";
import commonConfig from "@/config";
export default {
  inject: {
    uForm: {
      default: () => ({}),
    },
  },
  props: {
    item: {
      typeof: Object,
      default: () => {
        return {};
      },
    },
    data: {
      typeof: Object,
      default: () => {
        return {};
      },
    },
    value: {
      typeof: [String, Number, Boolean, Array, Object],
      default: () => {
        return "";
      },
    },
  },
  data() {
    return {
      // inputValue: "",
      show: false,
      //   fileList: [],
    };
  },
  computed: {
    disabled() {
      return this.uForm.disabled;
    },

    accept() {
      return this.item?.attrs?.accept || "image";
    },
    maxCount() {
      return this.item?.attrs?.maxCount || 1;
    },
    fileList: {
      get() {
        if (!this.value || this.value == "") {
          return [];
        }
        return this.value.split(";").map((e) => {
          return {
            url: e,
          };
        });
      },
      set(val) {
        let v = val.map((e) => e.url).join(";");
        this.$emit("input", v);
        uni.$u.formValidate(this.$refs.formitem, "blur");
      },
    },
  },
  methods: {
    deletePic(event) {
      this.fileList.splice(event.index, 1);
      this.$emit("input", this.fileList.map((e) => e.url).join(";"));

      uni.$u.formValidate(this.$refs.formitem, "blur");
    },
    blur() {
      this.$emit("input", this.inputValue);
    },
    select(e) {
      this.$emit("input", String(e.value));
      this.show = false;
    },
    afterRead(file) {
      upload({
        url: commonConfig.baseApi + "/infra/file/upload",
        filePath: file.file.url,
      }).then((e) => {
        this.fileList = [...this.fileList, { url: e.data }];
      });
    },
  },
};
</script>

<style scoped lang="scss">
.uni-input-input {
  font-size: 14px !important;
  color: #171a1d !important;
  line-height: 99rpx !important;
  height: 99rpx !important;
  padding-left: 10px;
}

.uni-forms-item__label {
  margin-left: 0;
  font-size: 14px !important;
  color: #171a1d !important;
  line-height: 99rpx !important;
  height: 99rpx !important;
}
</style>
